package com.example.micro.thread.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class CompletableFutureDemo {

    public static void main(String[] args) {
        CompletableFuture<Integer> ad = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        });
        System.out.println(ad);
        try {
            //getNow()不会阻塞，直接返回valueIfAbsent，即入参值；
            //get()阻塞获取结果
            System.out.println(ad.getNow(0));
            int dd = ad.get();
            System.out.println(dd);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

}
